package com.ruoyi.tws.service.impl;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.constant.WebsocketConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.tws.api.domain.Order;
import com.ruoyi.tws.domain.BillOrder;
import com.ruoyi.tws.domain.TwsCompany;
import com.ruoyi.tws.mapper.BillOrderMapper;
import com.ruoyi.tws.mapper.OrderMapper;
import com.ruoyi.tws.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.web.controller.BaseServiceImpl;
import com.ruoyi.tws.mapper.BillMapper;
import com.ruoyi.tws.domain.Bill;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 发票Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-13
 */
@Service
public class BillServiceImpl extends BaseServiceImpl<BillMapper, Bill> implements IBillService {
    @Autowired
    private ITwsCompanyService iTwsCompanyService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private IBillOrderService iBillOrderService;
    @Autowired
    private BillOrderMapper billOrderMapper;
    @Autowired
    private ITwsNoticeService twsNoticeService;
    @Autowired
    private RemoteUserService userService;

    /**
     * 申请开票
     * @param bill
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveBill(Bill bill) {
        TwsCompany company = iTwsCompanyService.getCompanyByUserId(SecurityUtils.getUserId());
        bill.setCompanyName(company.getCompanyName());
        save(bill);

        for (String orderId : bill.getOrderIds()) {
            System.err.println(orderId);
            //修改订单表开票状态
            Order order = new Order();
            order.setId(Long.parseLong(orderId));
            order.setIsBill("1");
            order.setUpdateBy(SecurityUtils.getUserId()+"");
            order.setUpdateTime(new Date());
            orderMapper.updateById(order);

            //中间表
            BillOrder billOrder = new BillOrder();
            billOrder.setBillId(bill.getId());
            billOrder.setOrderId(Long.parseLong(orderId));
            iBillOrderService.save(billOrder);
        }
        twsNoticeService.newTwsNotice("admin", WebsocketConstants.TWS_NOTICE_TYPE.CERT_BILL.getKey(), WebsocketConstants.TWS_NOTICE_TYPE.CERT_BILL.getName());
        return true;
    }

    /**
     * 通过开票
     * @param bill
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateBill(Bill bill) {
        bill.setBillUserId(SecurityUtils.getUserId());
        bill.setBillUser(SecurityUtils.getUsername());
        bill.setBillDate(new Date());
        bill.setStatus(2);
        updateById(bill);
        R<SysUser> sysUser = userService.infoById(Long.parseLong(bill.getCreateBy()), SecurityConstants.INNER);
        twsNoticeService.newTwsNotice(sysUser.getData().getUserName(), WebsocketConstants.TWS_NOTICE_TYPE.CERT_BILL_PASS.getKey(), WebsocketConstants.TWS_NOTICE_TYPE.CERT_BILL_PASS.getName());
        return true;
    }

    /**
     * 撤回开票
     * @param bill
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean recallBill(Bill bill) {
        List<BillOrder> billOrder = billOrderMapper.getByBillId(bill.getId());
        for (BillOrder bo: billOrder) {
            Order order = new Order();
            order.setId(bo.getOrderId());
            order.setIsBill("0");
            orderMapper.updateById(order);

            bo.setDelFlag(1);
            billOrderMapper.updateById(bo);
        }
        twsNoticeService.newTwsNotice("admin", WebsocketConstants.TWS_NOTICE_TYPE.CERT_BILL_FAIL.getKey(), WebsocketConstants.TWS_NOTICE_TYPE.CERT_BILL_FAIL.getName());

        return true;
    }
}
